<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 人员管理
 * Class Documentlist
 * @package app\store\controller
 */
class Member extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StoreMember';
    protected $table1 = 'store_member';

    /**
     * 人员管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '人员管理';
        $query = $this->_query($this->table)->like('username,phone,nickname,id_card,email');
        $query->dateBetween('create_at')->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {

    }

    /**
     * 记录
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function fenyong()
    {
        $this->title = '记录';
        $user_id = input("user_id");
        $query = $this->_query("store_fenyong")->where(['user_id'=>$user_id]);
        $query->dateBetween('create_at')->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _fenyong_page_filter(&$data)
    {
        foreach ($data as $k =>&$v){
            $v['member'] = Db::table('store_member')->where(['id'=>$v['user_id']])->find();
            if($v['is_jiesuan']==0){
                $v['status_name'] = '未结算';
            }else{
                $v['status_name'] = '已结算';
            }
        }
    }


    /**
     * 财务统计
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function tongji()
    {
        $this->title = '财务统计';
        $query = $this->_query($this->table)->like('username,phone,nickname,id_card,email')->where(['is_admin'=>1]);

        $this->charge_user_count = count(Db::table("store_member_amount_log")->where("lx=0")->field("user_id")->group("user_id")->select());
        $this->charge_total = Db::table("store_member_amount_log")->where("lx=0 and is_pay=1")->sum('amount');
        $this->pay_total = Db::table("store_member_amount_log")->where("lx=1 and is_pay=1")->sum('amount');
        $this->tixian_total = Db::table("store_member_amount_log")->where("lx=2 and is_pay=1")->sum('amount');

        $this->leiji_fenyong = Db::table("store_fenyong")->where("user_id!=0")->sum('amount');
        $this->leiji_charge = Db::table("store_fenyong")->where("user_id!=0")->sum('charge_price');
        $this->leiji_service = Db::table("store_fenyong")->where("user_id!=0")->sum('service_price');
        $this->leiji_park = Db::table("store_fenyong")->where("user_id!=0")->sum('park_price');

        $this->wait_jiesuan = Db::table("store_fenyong")->where("is_jiesuan=0 and user_id!=0")->sum('amount');
        $this->yi_jiesuan = Db::table("store_fenyong")->where("is_jiesuan=1 and user_id!=0")->sum('amount');


        $this->lirun_fenyong = Db::table("store_fenyong")->where("user_id=0")->sum('amount');
        $this->lirun_charge = Db::table("store_fenyong")->where("user_id=0")->sum('charge_price');
        $this->lirun_service = Db::table("store_fenyong")->where("user_id=0")->sum('service_price');
        $this->lirun_park = Db::table("store_fenyong")->where("user_id=0")->sum('park_price');

        $this->total_fenyong = Db::table("store_fenyong")->sum('amount');
        $this->total_charge = Db::table("store_fenyong")->sum('charge_price');
        $this->total_service = Db::table("store_fenyong")->sum('service_price');
        $this->total_park = Db::table("store_fenyong")->sum('park_price');

        $query->dateBetween('create_at')->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _tongji_page_filter(&$data)
    {
        foreach ($data as $k =>&$v){
            $v['station'] = Db::table("store_power_station")->field("type,name,phone")->where(['user_id'=>$v['id']])->find();
            if(empty($v['station'])){
                $v['type_name'] = '';
            }else{
                if($v['station']['type']==1){
                    $v['type_name'] = '物业';
                }
                if($v['station']['type']==2){
                    $v['type_name'] = '个人';
                }
                if($v['station']['type']==3){
                    $v['type_name'] = '联营';
                }
                if($v['station']['type']==4){
                    $v['type_name'] = '非联营';
                }
            }

            $v['total_fenyong'] = Db::table("store_fenyong")->where(['user_id'=>$v['id']])->sum('amount');
            $v['wait_fenyong'] = Db::table("store_fenyong")->where(['user_id'=>$v['id']])->where("is_jiesuan=0")->sum('amount');
            $v['yi_fenyong'] = Db::table("store_fenyong")->where(['user_id'=>$v['id']])->where("is_jiesuan=1")->sum('amount');

        }
    }

    public function jiesuan(){
        Db::table("store_fenyong")->where("is_jiesuan=0")->update(['is_jiesuan'=>1,'jiesuan_time'=>date('Y-m-d H:i:s')]);
    }

    public function dan_jiesuan(){
        $user_id = $_REQUEST['id'];
        Db::table("store_fenyong")->where("is_jiesuan=0 and user_id=$user_id")->update(['is_jiesuan'=>1,'jiesuan_time'=>date('Y-m-d H:i:s')]);
        $this->success(lang('think_library_save_success'), '');
    }

}
